package com.changgou.handler;


import com.changgou.entity.ExceptionResultCode;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.exception.BusinessException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 统一异常处理类
 */

@ControllerAdvice
public class UnifiedExceptionHandler {

    /**
     * 系统异常处理
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result systemException(Exception e){
        System.out.println("系统异常处理");
        e.printStackTrace();
        return new Result(false, StatusCode.ERROR, "当前系统繁忙，请稍后再试！");
    }


    /**
     * 业务异常处理
     * @return
     */
    @ExceptionHandler(value = BusinessException.class)
    @ResponseBody
    public Result bussinessException(BusinessException e){
        System.out.println("业务异常处理");
        e.printStackTrace();
        ExceptionResultCode resultCode = e.getExceptionResultCode();
        return new Result(resultCode.isFlag(), resultCode.getCode(), resultCode.getMessage());
    }
}
